// admin/system/service/sys_post_service.go
package service

import (
	"errors"
	repo "go-admin1/admin/system/dao"
	"go-admin1/admin/system/model"
)

// SysPostService 岗位服务接口
type SysPostService interface {
	Create(post *model.SysPost) error
	GetByID(id int64) (*model.SysPost, error)
	Update(post *model.SysPost) error
	Delete(id int64) error
	List(page, pageSize int) ([]*model.SysPost, int64, error)
	ListAll() ([]*model.SysPost, error)
	ListByUserID(userID int64) ([]*model.SysPost, error)
}

type sysPostService struct {
	postDao repo.SysPostDao
}

func (s sysPostService) Create(post *model.SysPost) error {
	return s.postDao.Create(post)
}

func (s sysPostService) GetByID(id int64) (*model.SysPost, error) {
	post, err := s.postDao.GetByID(id)
	if err != nil {
		return nil, err
	}
	return post, nil
}

func (s sysPostService) Update(post *model.SysPost) error {
	existingPost, err := s.postDao.GetByID(post.PostID)
	if err != nil {
		return err
	}
	if existingPost == nil {
		return errors.New("岗位不存在")
	}

	return s.postDao.Update(post)
}

func (s sysPostService) Delete(id int64) error {
	existingPost, err := s.postDao.GetByID(id)
	if err != nil {
		return err
	}
	if existingPost == nil {
		return errors.New("岗位不存在")
	}

	return s.postDao.Delete(id)
}

func (s sysPostService) List(page, pageSize int) ([]*model.SysPost, int64, error) {
	posts, total, err := s.postDao.List(page, pageSize)
	if err != nil {
		return nil, 0, err
	}
	return posts, total, nil
}

func (s sysPostService) ListAll() ([]*model.SysPost, error) {
	posts, err := s.postDao.ListAll()
	if err != nil {
		return nil, err
	}
	return posts, nil
}

func (s sysPostService) ListByUserID(userID int64) ([]*model.SysPost, error) {
	posts, err := s.postDao.ListByUserID(userID)
	if err != nil {
		return nil, err
	}
	return posts, nil
}

// NewSysPostService 创建岗位服务实例
func NewSysPostService() SysPostService {
	return &sysPostService{
		postDao: repo.NewSysPostDao(),
	}
}
